import {store} from "core-js/internals/reflect-metadata";
let COS = require('cos-js-sdk-v5');
import {AxiosNetwork} from "./axios";
let bucket = 'zhiran-1308915547'
let region = 'ap-guangzhou'

let cos = new COS({
    getAuthorization(options,callback){
      AxiosNetwork('GET','/resource/getSecret',{
        filename:"logo.png",
        zrId:"zr0000001"
      },null,res=>{
        let tempSecret = res.data
        console.log("tempSecret="+res.data.toString)
        callback({
          TmpSecretId: tempSecret.tempSecretId,
          TmpSecretKey: tempSecret.tempSecretKey,
          SecurityToken: tempSecret.sessionToken,
          StartTime: tempSecret.startTime, // 时间戳，单位秒，如：1580000000
          ExpiredTime: tempSecret.expiredTime, // 时间戳，单位秒，如：1580000000
        })
      },err=>{
        console.log(err.toString())
      })
    }
  // SecretId: store.state.zrSecret.secretId,
  // SecretKey: store.state.zrSecret.secretKey,
  // SecurityToken: store.state.zrSecret.securityToken
})

//上传小文件
export const cosSmallFileUpload =(key,file)=> cos.putObject({
  Bucket: bucket,
  Region: region,
  Key:key,
  StorageClass:'STANDARD',
  Body:file,
  onProgress(progressData){
    console.log(JSON.stringify(progressData))
  }
},(err,data)=>{
  console.log(err||data)
})

export const cosUploadFile=(key,file)=>cos.uploadFile({
  Bucket: bucket,
  Region: region,
  Key:key,
  Body:file,
  onTaskReady(taskId){
    console.log(taskId)
  },

  onProgress(progressData){
    console.log(JSON.stringify(progressData))
  },
  onFileFinish: function (err, data, options) {   /* 非必须 */
    console.log(options.Key + '上传' + (err ? '失败' : '完成'));
  },

}, (err,data)=>{
  console.log(err||data)
})

export const cosDownloadFIle = (key)=>cos.getObjectUrl({
  Bucket:bucket,
  Region: region,
  Key:key,
  Sign:true
}, (err,data)=>{
  if (err){
    return console.log(err)
  }
  let url = data.Url
  let downloadUrl = url + (url.indexOf('?') > -1 ? '&' : '?') + 'response-content-disposition=attachment'; // 补充强制下载的参数
  console.log("downloadUrl="+downloadUrl)
  window.open(downloadUrl)
})

//删除文件
export const cosDelete =(key)=> cos.deleteObject({
  Bucket:bucket,
  Region:region,
  Key:key
},(err,data)=>{
  console.log(err||data)
})


